package com.ff.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.common.R;
import com.ff.dto.SetmealDto;
import com.ff.entity.Category;
import com.ff.entity.Setmeal;
import com.ff.entity.SetmealDish;
import com.ff.service.CategoryService;
import com.ff.service.SetmealDishService;
import com.ff.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries=true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();

        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(name!=null,"name",name);
        queryWrapper.orderByDesc("update_time");

        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<SetmealDto> setmealDtoList=new ArrayList<>();
        List<Setmeal> list = pageInfo.getRecords();
        for (Setmeal tmp:list){
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(tmp,setmealDto);
            QueryWrapper<Category> wrapper=new QueryWrapper<>();
            wrapper.eq("id",tmp.getCategoryId());
            Category category = categoryService.getOne(wrapper);
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }
    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries=true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    //根据条件查询套餐数据
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,"category_id",setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,"status",setmeal.getStatus());
        queryWrapper.orderByDesc("update_time");
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
